package com.rtx.edi.platform.field.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.field.po.PlFieldMapping;
import com.rtx.edi.platform.field.mapper.PlFieldMappingMapper;
import com.rtx.edi.platform.field.service.PlFieldMappingService;

@Service
public class PlFieldMappingServiceImpl extends ServiceImpl<PlFieldMappingMapper, PlFieldMapping> implements PlFieldMappingService{

	@DataSourceAnnotation
	@Override
	public List<PlFieldMapping> queryByCode(String datasourcekey, String code) {
		LambdaQueryWrapper<PlFieldMapping> qw = Wrappers.lambdaQuery();
		qw.eq(PlFieldMapping::getCode, qw);
		return Optional.ofNullable(baseMapper.selectList(qw)).orElse(new ArrayList<PlFieldMapping>());
	}

	@DataSourceAnnotation
	@Override
	public List<PlFieldMapping> queryByCondition(String datasourcekey, LambdaQueryWrapper<PlFieldMapping> qw) {
		return Optional.ofNullable(baseMapper.selectList(qw)).orElse(new ArrayList<PlFieldMapping>());
	}

	@DataSourceAnnotation
	@Override
	public String queryWmsDataType(String datasourcekey,String type) {
		LambdaQueryWrapper<PlFieldMapping> qw = Wrappers.lambdaQuery();
		qw.eq(PlFieldMapping::getCode, "wms-type-mapping");
		qw.eq(PlFieldMapping::getSource, type);
		List<PlFieldMapping> list =	baseMapper.selectList(qw);
		if(list == null || list.size() < 1) {
			throw new RuntimeException("type-"+type+"类型在wms中未维护，请联系管理员维护-wms-type-mapping");
		}
		
		String wmsDataType ="";
		if(!StringUtils.hasLength(list.get(0).getUdf1())) {
			throw new RuntimeException("type-"+type+"类型在wms中未维护，请联系管理员维护-wms-type-mapping中wms单据类型大类");
		}
		
		wmsDataType = list.get(0).getUdf1();
		return wmsDataType;
	}

	@Override
	public void selectPageList(Page<PlFieldMapping> pageParam, PlFieldMapping po) {
		LambdaQueryWrapper<PlFieldMapping> query =Wrappers.lambdaQuery();
		if(po != null) {
			query.like(po.getCode()!=null,PlFieldMapping::getCode, po.getCode());
			query.like(po.getName()!=null,PlFieldMapping::getName, po.getName());
			query.eq(po.getSource()!=null,PlFieldMapping::getSource, po.getSource());
			query.eq(po.getTarget()!=null,PlFieldMapping::getTarget, po.getTarget());
			query.eq(po.getUdf1()!=null,PlFieldMapping::getUdf1, po.getUdf1());
			query.eq(po.getUdf2()!=null,PlFieldMapping::getUdf2, po.getUdf2());
			query.eq(po.getUdf3()!=null,PlFieldMapping::getUdf3, po.getUdf3());
			query.eq(po.getUdf4()!=null,PlFieldMapping::getUdf4, po.getUdf4());
			query.eq(po.getUdf5()!=null,PlFieldMapping::getUdf5, po.getUdf5());
			query.eq(po.getUdf6()!=null,PlFieldMapping::getUdf6, po.getUdf6());
			query.eq(po.getUdf7()!=null,PlFieldMapping::getUdf7, po.getUdf7());
			query.eq(po.getUdf8()!=null,PlFieldMapping::getUdf8, po.getUdf8());
			query.eq(po.getUdf9()!=null,PlFieldMapping::getUdf9, po.getUdf9());
			query.eq(po.getUdf10()!=null,PlFieldMapping::getUdf10, po.getUdf10());
		}
		
		baseMapper.selectPage(pageParam,query);
	}

	@DataSourceAnnotation
	@Override
	public void insertFieldMapping(String datasourcekey, PlFieldMapping po) {
		baseMapper.insert(po);
	}
}
